﻿using GrayParrot.Core;
using GrayParrot.Infrastructure;
using GrayParrot.Web.Formly;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using WebTest.Facades;
using WebTest.Metadata;

namespace WebTest.Controllers
{
    public class TestController : ApiController
    {
        [HttpGet]
        public string LoadFields()
        {
            string retValue = null;

            var m_fields = FormlyHelper.ToFormly<CambioDataMetadata>(typeof(CambioDataMetadata));

            //DateTime m_date = DateTime.Now.ToLocalTime();
            //DateTime m_date = new DateTime(2016, 12, 8, 20, 30, 00);
            //m_fields.DefaultValue<DateTime>("DataAvvenimento", m_date);

            retValue = FormlyHelper.Serialize(m_fields);

            return retValue;
        }

        [HttpPost]
        public System.Net.Http.HttpResponseMessage CambioData(JObject param)
        {
            HttpResponseMessage retValue = new HttpResponseMessage(HttpStatusCode.BadRequest);

            dynamic json = param;
            int m_palinsesto = int.Parse(json.txtPalinsesto.ToString());
            int m_avvenimento = int.Parse(json.txtAvvenimento.ToString());
            DateTime m_nuovaData = DateTime.Parse(json.DataMatch.ToString());
            bool m_forzaInvio = (json.ForzaInvio == "true" ? true : false);

            Console.WriteLine(m_palinsesto);
            Console.WriteLine(m_avvenimento);
            Console.WriteLine(m_nuovaData);
            Console.WriteLine(m_forzaInvio);

            return retValue;
        }

        [HttpGet]
        public IEnumerable<NameValue<string, int>> LoadShippers()
        {
            IEnumerable<NameValue<string, int>> retValue = null;

            using (TestFacade m_cf = new TestFacade())
            {
                m_cf.Initialize();
                retValue = m_cf.GetShippers();
            }

            return retValue;
        }
    }
}